package com.sc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.domain.PayItem;
import com.sc.dto.PayItemPageQueryDTO;
import com.sc.mapper.PayItemMapper;
import com.sc.result.PageResult;
import com.sc.service.PayItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class PayItemServiceImpl extends ServiceImpl<PayItemMapper,PayItem> implements PayItemService {
    @Autowired
    PayItemMapper payItemMapper;


    @Override
    public PageResult<PayItem> pageQueryItem(PayItemPageQueryDTO requestParam) {
        // 1. 构建分页对象
        Page<PayItem> page = new Page<>(requestParam.getPage(), requestParam.getPageSize());

        // 2. 构建查询条件
        LambdaQueryWrapper<PayItem> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(requestParam.getItemName())) {
            queryWrapper.like(PayItem::getItemName, requestParam.getItemName()); // 按项目名称模糊查询
        }
        if (StringUtils.hasText(requestParam.getChargeMethod())) {
            queryWrapper.eq(PayItem::getChargeMethod, requestParam.getChargeMethod()); // 按收费方式精确查询
        }

        // 3. 执行分页查询
        Page<PayItem> resultPage = this.page(page, queryWrapper);

        // 4. 封装分页结果
        return new PageResult<>(resultPage.getTotal(), resultPage.getRecords());
    }

    @Override
    public PayItem getByName(String name) {
        QueryWrapper<PayItem> wrapper = new QueryWrapper<>();
        wrapper.eq("item_name", name);

        PayItem payItem = getOne(wrapper);
        return payItem;
    }
}
